package sanrenxing.foreMain.servlet;

import com.google.gson.Gson;
import sanrenxing.foreMain.dao.NoticeShowDao;
import sanrenxing.foreMain.dao.impl.NoticeShowDaoImpl;
import sanrenxing.foreMain.domain.NoticeShow;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @date 2020/10/14 22:32
 * 编辑者：邓志强
 * 功能：
 */
@WebServlet("/NoticeShowServlet")
public class NoticeShowServlet extends HttpServlet {
    //创建数据层对象
    NoticeShowDao nsd=new NoticeShowDaoImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求字符集
        req.setCharacterEncoding("utf-8");
        //设置响应字符集和内容类型
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        //接收来自客户端方法信息
        String method=req.getParameter("method");
        if (method.equals("showNotice")){
            this.showNotice(req,resp);
        }
    }

    /**
     * 显示公告信息的方法
     * @param req
     * @param resp
     */
    private void showNotice(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //创建输出流
        PrintWriter out=resp.getWriter();
        //通过session拿到学员编号
        HttpSession session=req.getSession();
        String stuId=(String) session.getAttribute("stuId");
        //查询出对应班级的公告信息
        List<NoticeShow> noticeShowList=nsd.findNotice(stuId);
        if (noticeShowList.size()!=0){
            //创建map集合
            Map<String,Object> map=new HashMap<>();
            map.put("data",noticeShowList);
            //创建json对象
            Gson gson=new Gson();
            String json=gson.toJson(map);
            out.print(json);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
